package com.ruoyi.fantasy.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.fantasy.common.WorldInit;
import com.ruoyi.fantasy.domain.FantasyNpc;
import com.ruoyi.fantasy.domain.FantasyNpcPosition;
import com.ruoyi.fantasy.domain.FantasyNpcPositionBO;
import com.ruoyi.fantasy.mapper.FantasyNpcPositionMapper;
import com.ruoyi.fantasy.utils.RedisUtil;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyNpcMapper;
import com.ruoyi.fantasy.service.IFantasyNpcService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * npc 服务层实现
 * 
 * @author wanghui
 * @date 2019-08-02
 */
@Service
public class FantasyNpcServiceImpl implements IFantasyNpcService 
{
	@Resource
	private FantasyNpcMapper fantasyNpcMapper;

	@Resource
	private FantasyNpcPositionMapper fantasyNpcPositionMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询npc信息
     * 
     * @param npcId npcID
     * @return npc信息
     */
    @Override
	public FantasyNpc selectFantasyNpcById(String npcId)
	{
	    return fantasyNpcMapper.selectFantasyNpcById(npcId);
	}
	
	/**
     * 查询npc列表
     * 
     * @param fantasyNpc npc信息
     * @return npc集合
     */
	@Override
	public List<FantasyNpc> selectFantasyNpcList(FantasyNpc fantasyNpc)
	{
	    return fantasyNpcMapper.selectFantasyNpcList(fantasyNpc);
	}
	
    /**
     * 新增npc
     * 
     * @param fantasyNpc npc信息
     * @return 结果
     */
	@Override
	public int insertFantasyNpc(FantasyNpc fantasyNpc)
	{
	    return fantasyNpcMapper.insertFantasyNpc(fantasyNpc);
	}
	
	/**
     * 修改npc
     * 
     * @param fantasyNpc npc信息
     * @return 结果
     */
	@Override
	public int updateFantasyNpc(FantasyNpc fantasyNpc) {
		Integer result = fantasyNpcMapper.updateFantasyNpc(fantasyNpc);
		if (result > 0) {
			// 需要把修改时没有传的npc头像获取到，才能更新缓存
			FantasyNpc npcFace = fantasyNpcMapper.selectFantasyNpcById(fantasyNpc.getNpcId());

			FantasyNpcPosition queryNpcPosition = new FantasyNpcPosition();
			queryNpcPosition.setNpcId(fantasyNpc.getNpcId());
			List<FantasyNpcPosition> fantasyNpcPositionList = fantasyNpcPositionMapper.selectFantasyNpcPositionList(queryNpcPosition);
			FantasyNpcPosition fantasyNpcPosition = fantasyNpcPositionList.get(0);
			FantasyNpcPositionBO fantasyNpcPositionBO = new FantasyNpcPositionBO();
			fantasyNpcPositionBO.setMapId(fantasyNpcPosition.getMapId());
			fantasyNpcPositionBO.setNpcCommonTalkStatus(fantasyNpc.getNpcCommonTalkStatus());
			fantasyNpcPositionBO.setNpcCommonTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			fantasyNpcPositionBO.setNpcDesc(fantasyNpc.getNpcDesc());
			fantasyNpcPositionBO.setNpcFace(npcFace.getNpcFace());
			fantasyNpcPositionBO.setNpcId(fantasyNpcPosition.getNpcId());
			fantasyNpcPositionBO.setNpcName(fantasyNpc.getNpcName());
			fantasyNpcPositionBO.setNpcReplyStatus(fantasyNpc.getNpcReplyStatus());
			fantasyNpcPositionBO.setNpcSpecialTalkStatus(fantasyNpc.getNpcSpecialTalkStatus());
			fantasyNpcPositionBO.setNpcSpecialTaskStatus(fantasyNpc.getNpcSpecialTaskStatus());
			fantasyNpcPositionBO.setPositionId(fantasyNpcPosition.getPositionId());
			fantasyNpcPositionBO.setTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			Object hGet = redisUtil.hGet(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId());
			if (null != hGet) {
				List<FantasyNpcPositionBO> npclist = JSONObject.parseArray(String.valueOf(hGet), FantasyNpcPositionBO.class);
				npclist.removeIf(filterNpcPositionBO -> filterNpcPositionBO.getNpcId().equals(fantasyNpcPosition.getNpcId()));
				npclist.add(fantasyNpcPositionBO);
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId(), JSON.toJSONString(npclist));
			}
		}
	    return result;
	}

	/**
     * 删除npc对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyNpcByIds(String ids)
	{
		return fantasyNpcMapper.deleteFantasyNpcByIds(Convert.toStrArray(ids));
	}

	/**
	 * 转换列表中的值为对应的内容
	 * @param list
	 */
	@Override
	public void transforData(List<FantasyNpc> list) {
		Map<String, SysUser> sysUserMap = WorldInit.sysUserMap;
		for (FantasyNpc fantasyNpc : list) {
			if (fantasyNpc.getCreateBy() != null) {
				if (sysUserMap.get(fantasyNpc.getCreateBy()) != null) {
					fantasyNpc.setCreateBy(sysUserMap.get(fantasyNpc.getCreateBy()).getUserName());
				} else {
					fantasyNpc.setCreateBy("系统管理员");
				}
			}
			if (fantasyNpc.getUpdateBy() != null) {
				if (sysUserMap.get(fantasyNpc.getUpdateBy()) != null) {
					fantasyNpc.setUpdateBy((sysUserMap.get(fantasyNpc.getUpdateBy()).getUserName()));
				} else {
					fantasyNpc.setUpdateBy("系统管理员");
				}
			}
			//禁用标记
			if (fantasyNpc.getDelFlag().equals("0")) {
				fantasyNpc.setDelFlag("正常");
			} else if (fantasyNpc.getDelFlag().equals("1")) {
				fantasyNpc.setDelFlag("禁用");
			}
			//npc通用话语
			if (fantasyNpc.getNpcCommonTalkStatus().equals(0)) {
				fantasyNpc.setNpcCommonTalkStr("否");
			} else if (fantasyNpc.getNpcCommonTalkStatus().equals(1)) {
				fantasyNpc.setNpcCommonTalkStr("是");
			}
			//npc专有话语
			if (fantasyNpc.getNpcSpecialTalkStatus().equals(0)) {
				fantasyNpc.setNpcSpecialTalkStr("否");
			} else if (fantasyNpc.getNpcSpecialTalkStatus().equals(1)) {
				fantasyNpc.setNpcSpecialTalkStr("是");
			}
			//npc问题答复
			if (fantasyNpc.getNpcReplyStatus().equals(0)) {
				fantasyNpc.setNpcReplyStr("否");
			} else if (fantasyNpc.getNpcReplyStatus().equals(1)) {
				fantasyNpc.setNpcReplyStr("是");
			}
			//通用任务发放
			if (fantasyNpc.getNpcCommonTaskStatus().equals(0)) {
				fantasyNpc.setNpcCommonTaskStr("否");
			} else if (fantasyNpc.getNpcCommonTaskStatus().equals(1)) {
				fantasyNpc.setNpcCommonTaskStr("是");
			}
			//专有任务发放
			if (fantasyNpc.getNpcSpecialTaskStatus().equals(0)) {
				fantasyNpc.setNpcSpecialTaskStr("否");
			} else if (fantasyNpc.getNpcSpecialTaskStatus().equals(1)) {
				fantasyNpc.setNpcSpecialTaskStr("是");
			}
		}

	}

	/**
	 * 根据条件分页查询已分配Npc列表
	 *
	 * @param fantasyNpc Npc信息
	 * @return Npc信息集合信息
	 */
	public List<FantasyNpc> selectAllocatedList(FantasyNpc fantasyNpc)
	{
		return fantasyNpcMapper.selectAllocatedList(fantasyNpc);
	}

	@Override
	public List<FantasyNpc> selectUnallocatedList(FantasyNpc fantasyNpc) {
		return fantasyNpcMapper.selectUnallocatedList(fantasyNpc);
	}
}
